﻿/******************************************************************************
 *  All Rights Reserved , Copyright (C) 2012 , EricHu. 
 *  作    者： EricHu
 *  创建时间： 2012-4-19 16:42:19
 ******************************************************************************/
using System;
using System.Data;
using System.Reflection;
using System.ServiceModel;

namespace RDIFramework.ServiceAdapter
{
    using RDIFramework.BusinessEntity;
    using RDIFramework.IService;
    using RDIFramework.Utilities;

    /// <summary>
    /// LogOnService
    /// 登录服务
    /// 
    /// <author>
    ///		<name>EricHu</name>
    ///		<date>2012.04.19</date>
    /// </author> 
    /// </summary>
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class LogOnService : System.MarshalByRefObject, ILogOnService
    {
        private readonly string serviceName = RDIFrameworkMessage.LogOnService;

        /// <summary>
        /// RDIFramework.NET框架数据库连接
        /// </summary>
        private readonly string RDIFrameworkDbConection = SystemInfo.RDIFrameworkDbConection;

        #region public DataTable GetUserDT(UserInfo userInfo):获得用户列表
        /// <summary>
        /// 获得用户列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <returns>数据表</returns>
        public DataTable GetUserDT(UserInfo userInfo)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            DataTable dataTable = new DataTable(PiUserTable.TableName);
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // 检查用户在线状态(服务器专用)
                    PiUserManager userManager = new PiUserManager(dbProvider, userInfo);
                    userManager.CheckOnLine();
                    // 获取允许登录列表
                    string[] names = new string[] { PiUserTable.FieldEnabled, PiUserTable.FieldDeleteMark };
                    Object[] values = new Object[] { 1, 0 };
                    dataTable = userManager.GetDT(names, values, PiUserTable.FieldSortCode);
                    dataTable.TableName = PiUserTable.TableName;
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.LogOnService_GetUserDT, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return dataTable;
        }
        #endregion

        #region public DataTable GetStaffUserDT(UserInfo userInfo):获得内部员工列表
        /// <summary>
        /// 获得内部员工列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <returns>数据表</returns>
        public DataTable GetStaffUserDT(UserInfo userInfo)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            DataTable dataTable = new DataTable(PiStaffTable.TableName);
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // 检查用户在线状态(服务器专用)
                    PiUserManager userManager = new PiUserManager(dbProvider);
                    userManager.CheckOnLine();
                    // 获取允许登录列表
                    string[] names = new string[] { PiUserTable.FieldEnabled, PiUserTable.FieldDeleteMark, PiUserTable.FieldIsStaff };
                    Object[] values = new Object[] { 1, 0, 1 };
                    dataTable = userManager.GetDT(names, values, PiStaffTable.FieldSortCode);
                    dataTable.TableName = PiUserTable.TableName;
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.LogOnService_GetStaffUserDT, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return dataTable;
        }
        #endregion

        #region public UserInfo AccountActivation(UserInfo userInfo, string openId, out string statusCode, out string statusMessage):激活用户
        /// <summary>
        /// 激活用户
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="openId">唯一识别码</param>
        /// <param name="statusCode">返回状态码</param>
        /// <param name="statusMessage">返回状消息</param>
        /// <returns>用户实体</returns>
        public UserInfo AccountActivation(UserInfo userInfo, string openId, out string statusCode, out string statusMessage)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            UserInfo returnUserInfo = null;
            statusCode = string.Empty;
            statusMessage = string.Empty;

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiUserManager userManager = new PiUserManager(dbProvider, userInfo);
                    // 先侦测是否在线
                    userManager.CheckOnLine();
                    // 再进行登录
                    returnUserInfo = userManager.AccountActivation(openId, out statusCode);
                    statusMessage = userManager.GetStateMessage(statusCode);
                    // 登录时会自动记录进行日志记录，所以不需要进行重复日志记录
                    // LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, "激活用户", MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return returnUserInfo;
        }
        #endregion

        #region public UserInfo LogOnByOpenId(UserInfo userInfo, string openId, out string statusCode, out string statusMessage):按唯一识别码登录
        /// <summary>
        /// 按唯一识别码登录
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="openId">唯一识别码</param>
        /// <param name="returnStatusCode">返回状态码</param>
        /// <param name="returnStatusMessage">返回状消息</param>
        /// <returns>用户实体</returns>
        public UserInfo LogOnByOpenId(UserInfo userInfo, string openId, out string returnStatusCode, out string returnStatusMessage)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                SystemInfo.IsAuthorized(userInfo);
            #endif

            UserInfo returnUserInfo = null;
            returnStatusCode = string.Empty;
            returnStatusMessage = string.Empty;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiUserManager userManager = new PiUserManager(dbProvider, userInfo);
                    // 先侦测是否在线
                    userManager.CheckOnLine();
                    // 再进行登录
                    returnUserInfo = userManager.LogOnByOpenId(openId, userInfo.IPAddress, userInfo.MACAddress);
                    returnStatusCode = userManager.ReturnStatusCode;
                    returnStatusMessage = userManager.GetStateMessage(userManager.ReturnStatusCode);
                    // 登录时会自动记录进行日志记录，所以不需要进行重复日志记录
                    // LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return returnUserInfo;
        }
        #endregion

        #region public UserInfo LogOnByUserName(UserInfo userInfo, string userName, out string returnStatusCode, out string returnStatusMessage):按唯一识别码登录
        /// <summary>
        /// 按用户名登录
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="userName">用户名</param>
        /// <param name="returnStatusCode">返回状态码</param>
        /// <param name="returnStatusMessage">返回状消息</param>
        /// <returns>用户实体</returns>
        public UserInfo LogOnByUserName(UserInfo userInfo, string userName, out string returnStatusCode, out string returnStatusMessage)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                SystemInfo.IsAuthorized(userInfo);
            #endif

            UserInfo returnUserInfo = null;
            returnStatusCode = string.Empty;
            returnStatusMessage = string.Empty;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiUserManager userManager = new PiUserManager(dbProvider, userInfo);
                    // 先侦测是否在线
                    userManager.CheckOnLine();
                    // 再进行登录
                    returnUserInfo = userManager.LogOnByUserName(userName, userInfo.IPAddress, userInfo.MACAddress);
                    returnStatusCode = userManager.ReturnStatusCode;
                    returnStatusMessage = userManager.GetStateMessage();
                    // 登录时会自动记录进行日志记录，所以不需要进行重复日志记录
                    // LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return returnUserInfo;
        }
        #endregion

        #region public UserInfo UserLogOn(UserInfo userInfo, string userName, string password, out string returnStatusCode, out string returnStatusMessage):用户登录
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="ipAddress">IP地址</param>
        /// <param name="statusCode">返回状态码</param>
        /// <param name="statusMessage">返回状消息</param>
        /// <returns>用户实体</returns>
        public UserInfo UserLogOn(UserInfo userInfo, string userName, string password, bool createOpenId, out string returnStatusCode, out string returnStatusMessage)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            returnStatusCode = StatusCode.DbError.ToString();
            returnStatusMessage = string.Empty;

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                SystemInfo.IsAuthorized(userInfo);
            #endif

            UserInfo returnUserInfo = null;
            // statusCode = ServiceSecurityContext.Current.PrimaryIdentity.Name;            

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiUserManager userManager = new PiUserManager(dbProvider, userInfo);
                    // 先侦测是否在线
                    userManager.CheckOnLine();
                    // 再进行登录
                    returnUserInfo      = userManager.LogOn(userName, password, createOpenId);
                    returnStatusCode    = userManager.ReturnStatusCode;
                    returnStatusMessage = userManager.GetStateMessage(returnStatusCode);
                    // 登录时会自动记录进行日志记录，此处不再重复日志记录
                    // LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart, ConsoleColor.Red);
            #endif

            return returnUserInfo;
        }
        #endregion

        #region public void OnLine(UserInfo userInfo):用户在线
        /// <summary>
        /// 用户在线
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="onLineState">用户在线状态</param>
        public void OnLine(UserInfo userInfo, int onLineState = 1)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.LogOnService_OnLine, MethodBase.GetCurrentMethod());
                    PiUserManager userManager = new PiUserManager(dbProvider, userInfo);               
                    userManager.OnLine(userInfo.Id, onLineState);                 
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart, ConsoleColor.Green);
            #endif
        }
        #endregion

        #region public void OnExit(UserInfo userInfo)：用户离线
        /// <summary>
        /// 用户离线
        /// </summary>
        /// <param name="userInfo">用户</param>
        public void OnExit(UserInfo userInfo)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.LogOnService_OnExit, MethodBase.GetCurrentMethod());
                    PiUserManager userManager = new PiUserManager(dbProvider, userInfo);
                    userManager.OnExit(userInfo.Id);
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart, ConsoleColor.Yellow);
            #endif
        }
        #endregion

        #region public int ServerCheckOnLine()：服务器端检查在线状态
        /// <summary>
        /// 服务器端检查在线状态
        /// </summary>
        /// <returns>离线人数</returns>
        public int ServerCheckOnLine()
        {
            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiUserManager userManager = new PiUserManager(dbProvider);
                    returnValue = userManager.CheckOnLine();
                }
                catch (Exception ex)
                {
                    LogHelper.WriteException(ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
            return returnValue;
        }
        #endregion

        #region public int SetPassword(UserInfo userInfo, string[] userIds, string password, out string returnStatusCode, out string returnStatusMessage):设置用户密码
        /// <summary>
        /// 设置用户密码
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="userId">被设置的员工主键</param>
        /// <param name="password">新密码</param>
        /// <param name="returnStatusCode">返回状态码</param>
        /// <param name="returnStatusMessage">返回状消息</param>
        /// <returns>影响行数</returns>
        public int SetPassword(UserInfo userInfo, string[] userIds, string password, out string returnStatusCode, out string returnStatusMessage)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            returnStatusCode = string.Empty;
            returnStatusMessage = string.Empty;
            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.LogOnService_SetPassword, MethodBase.GetCurrentMethod());
                    PiUserManager userManager = new PiUserManager(dbProvider, userInfo);
                    returnValue = userManager.BatchSetPassword(userIds, password);
                    returnStatusCode = userManager.ReturnStatusCode;
                    // 获得状态消息
                    returnStatusMessage = userManager.GetStateMessage(returnStatusCode);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.LogOnService_SetPassword, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return returnValue;
        }
        #endregion

        #region public int ChangePassword(UserInfo userInfo, string oldPassword, string newPassword, out string statusCode, out string statusMessage):用户修改密码
        /// <summary>
        /// 用户修改密码
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="oldPassword">原始密码</param>
        /// <param name="newPassword">新密码</param>
        /// <param name="statusCode">返回状态码</param>
        /// <param name="statusMessage">返回状消息</param>
        /// <returns>影响行数</returns>
        public int ChangePassword(UserInfo userInfo, string oldPassword, string newPassword, out string statusCode, out string statusMessage)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            statusCode = string.Empty;
            statusMessage = string.Empty;
            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // 事务开始
                    // dbProvider.BeginTransaction();
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.LogOnService_ChangePassword, MethodBase.GetCurrentMethod());
                    PiUserManager userManager = new PiUserManager(dbProvider, userInfo);
                    returnValue = userManager.ChangePassword(oldPassword, newPassword, out statusCode);
                    // 获得状态消息
                    statusMessage = userManager.GetStateMessage(statusCode);
                    // 事务提交
                    // dbProvider.CommitTransaction();
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.LogOnService_ChangePassword, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    // 事务回滚
                    // dbProvider.RollbackTransaction();
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return returnValue;
        }
        #endregion

        #region public int ChangeCommunicationPassword(UserInfo userInfo, string oldPassword, string newPassword, out string statusCode, out string statusMessage):用户修改通讯密码
        /// <summary>
        /// 用户修改通讯密码
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="oldPassword">原始密码</param>
        /// <param name="newPassword">新密码</param>
        /// <param name="statusCode">返回状态码</param>
        /// <param name="statusMessage">返回状消息</param>
        /// <returns>影响行数</returns>
        public int ChangeCommunicationPassword(UserInfo userInfo, string oldPassword, string newPassword, out string statusCode, out string statusMessage)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            statusCode = string.Empty;
            statusMessage = string.Empty;
            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // 事务开始
                    // dbProvider.BeginTransaction();
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.LogOnService_ChangeCommunicationPassword, MethodBase.GetCurrentMethod());
                    PiUserManager userManager = new PiUserManager(dbProvider, userInfo);
                    returnValue = userManager.ChangeCommunicationPassword(oldPassword, newPassword, out statusCode);
                    // 获得状态消息
                    statusMessage = userManager.GetStateMessage(statusCode);
                    // 事务提交
                    // dbProvider.CommitTransaction();
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.LogOnService_ChangeCommunicationPassword, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    // 事务回滚
                    // dbProvider.RollbackTransaction();
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return returnValue;
        }
        #endregion

        #region public bool CommunicationPassword(UserInfo userInfo, string communicationPassword):验证用户通讯密码
        /// <summary>
        /// 验证用户通讯密码
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="communicationPassword">通讯密码</param>
        /// <param name="ipAddress">IP地址</param>
        /// <param name="statusCode">返回状态码</param>
        /// <param name="statusMessage">返回状消息</param>
        /// <returns>是否正确</returns>
        public bool CommunicationPassword(UserInfo userInfo, string communicationPassword)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            bool returnValue = false;

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiUserManager userManager = new PiUserManager(dbProvider, userInfo);
                    returnValue = userManager.CommunicationPassword(communicationPassword);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.LogOnService_CommunicationPassword, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return returnValue;
        }
        #endregion

        #region public int SetCommunicationPassword(UserInfo userInfo, string[] userIds, string password, out string statusCode, out string statusMessage):设置用户通讯密码
        /// <summary>
        /// 设置用户通讯密码
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="userId">被设置的员工主键</param>
        /// <param name="password">新密码</param>
        /// <param name="statusCode">返回状态码</param>
        /// <param name="statusMessage">返回状消息</param>
        /// <returns>影响行数</returns>
        public int SetCommunicationPassword(UserInfo userInfo, string[] userIds, string password, out string statusCode, out string statusMessage)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            statusCode = string.Empty;
            statusMessage = string.Empty;
            int returnValue = 0;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.LogOnService_SetPassword, MethodBase.GetCurrentMethod());
                    PiUserManager userManager = new PiUserManager(dbProvider, userInfo);
                    //returnValue = userManager.BatchSetCommunicationPassword(userIds, password, out statusCode);
                    // 获得状态消息
                    statusMessage = userManager.GetStateMessage(statusCode);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.LogOnService_SetCommunicationPassword, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return returnValue;
        }
        #endregion

        #region public string CreateDigitalSignature(UserInfo userInfo, string password, out string returnStatusCode, out string returnStatusMessage):创建数字证书签名
        /// <summary>
        /// 创建数字证书签名
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="password">密码</param>
        /// <param name="returnStatusCode">返回状态码</param>
        /// <param name="returnStatusMessage">返回状消息</param>
        /// <returns>私钥</returns>
        public string CreateDigitalSignature(UserInfo userInfo, string password, out string returnStatusCode, out string returnStatusMessage)
        {
            string returnValue = string.Empty;
            returnStatusCode = string.Empty;
            returnStatusMessage = string.Empty;

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiUserManager userManager = new PiUserManager(dbProvider, userInfo);
                    returnValue = userManager.CreateDigitalSignature(password, out returnStatusCode);
                    // 获得状态消息
                    returnStatusMessage = userManager.GetStateMessage(returnStatusCode);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.LogOnService_CreateDigitalSignature, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
            return returnValue;
        }
        #endregion

        #region public string GetPublicKey(UserInfo userInfo, string userId):获取当前用户的公钥
        /// <summary>
        /// 获取当前用户的公钥
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <returns>公钥</returns>
        public string GetPublicKey(UserInfo userInfo, string userId)
        {
            string returnValue = string.Empty;

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiUserManager userManager = new PiUserManager(dbProvider, userInfo);
                    returnValue = userManager.GetPublicKey(userId);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.LogOnService_GetPublicKey, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            return returnValue;
        }
        #endregion

        #region public int ChangeSignedPasswordd(UserInfo userInfo, string oldPassword, string newPassword, out string statusCode, out string statusMessage)
        /// <summary>
        /// 用户修改签名密码
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="oldPassword">原始密码</param>
        /// <param name="newPassword">新密码</param>
        /// <param name="statusCode">返回状态码</param>
        /// <param name="statusMessage">返回状消息</param>
        /// <returns>影响行数</returns>
        public int ChangeSignedPassword(UserInfo userInfo, string oldPassword, string newPassword, out string statusCode, out string statusMessage)
        {
            statusCode = string.Empty;
            statusMessage = string.Empty;
            int returnValue = 0;

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // 事务开始
                    // dbProvider.BeginTransaction();
                    PiUserManager userManager = new PiUserManager(dbProvider, userInfo);
                    returnValue = userManager.ChangeSignedPassword(oldPassword, newPassword, out statusCode);
                    // 获得状态消息
                    statusMessage = userManager.GetStateMessage(statusCode);
                    // 事务提交
                    // dbProvider.CommitTransaction();
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.LogOnService_ChangeSignedPassword, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    // 事务回滚
                    // dbProvider.RollbackTransaction();
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            return returnValue;
        }
        #endregion

        #region public bool SignedPassword(UserInfo userInfo, string signedPassword):验证用户数字签名密码
        /// <summary>
        /// 验证用户数字签名密码
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="signedPassword">验证数字签名密码</param>
        /// <param name="ipAddress">IP地址</param>
        /// <param name="statusCode">返回状态码</param>
        /// <param name="statusMessage">返回状消息</param>
        /// <returns>是否正确</returns>
        public bool SignedPassword(UserInfo userInfo, string signedPassword)
        {
            bool returnValue = false;

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiUserManager userManager = new PiUserManager(dbProvider, userInfo);
                    returnValue = userManager.SignedPassword(signedPassword);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.LogOnService_SignedPassword, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            return returnValue;
        }
        #endregion

        #region public static bool UserIsLogOn(UserInfo userInfo):用户是否已经登录了系统？
        /// <summary>
        /// 用户是否已经登录了系统？
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <returns>是否登录</returns>
        public static bool UserIsLogOn(UserInfo userInfo)
        {
            // 加强安全验证防止未授权匿名调用
            if (!SystemInfo.IsAuthorized(userInfo))
            {
                throw new Exception(RDIFrameworkMessage.MSG0800);
            }
            // 这里表示是没登录过的用户
            if (string.IsNullOrEmpty(userInfo.OpenId))
            {
                throw new Exception(RDIFrameworkMessage.MSG0900);
            }
            // 确认用户是否登录了？是否进行了匿名的破坏工作
            
            IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbConection);
            PiUserManager userManager = new PiUserManager(dbProvider, userInfo);

            if (!userManager.UserIsLogOn(userInfo))
            {
                throw new Exception(RDIFrameworkMessage.MSG0900);            
            }
            
            return true;
        }
        #endregion

    }
}
